package com.hp.service;

import com.hp.dao.FormMapper;
import com.hp.dao.RentingMapper;
import com.hp.dao.SeehouseapplicationMapper;
import com.hp.dao.UserMapper;
import com.hp.pojo.Form;
import com.hp.pojo.Renting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RentingService {
    @Autowired private RentingMapper rentingMapper;

    public Map<String ,Object> getAllRentings(){
        HashMap<String,Object> allRentings = new HashMap<>();
        allRentings.put("allRentings",rentingMapper.getAllRentings());
        return allRentings;
    }

    public Map<String,Object> getRentingsAll(){
        HashMap<String,Object> map=new HashMap<>();
        map.put("allRentings",rentingMapper.getRentingsAll());
        return map;
    }

    public Map<String, String> changeApply(int rentingId) {
        //修改某租房记录的申请状态
        Renting tempRenting = rentingMapper.getRentingById(rentingId);

        if(tempRenting.getRentingApply() == 1){
            tempRenting.setRentingApply(0);
        }
        else if(tempRenting.getRentingApply() == 0){
            tempRenting.setRentingApply(1);
        }
        if(tempRenting.getRentingState() == 1){
            tempRenting.setRentingState(0);
        }
        else if(tempRenting.getRentingState() == 0){
            tempRenting.setRentingState(1);
        }

        rentingMapper.updateRentingById(tempRenting);
        HashMap<String, String> res = new HashMap<>();
        res.put("message","修改成功");
        return res;
    }

    public Map<String, String> addRenting(Renting renting) {
        HashMap<String, String> res = new HashMap<>();
        try {
            int r = rentingMapper.addRenting(renting);
            res.put("message","添加成功");
            return res;
        }
        catch (Exception e){
            res.put("message","添加失败");
            e.printStackTrace();
            return res;
        }
    }

    public Map<String, Object> getAllRentingsByHouseId(int houseId) {
        List<Renting> allRentings = rentingMapper.getAllRentings();
        List<Renting> res = new ArrayList<>();
        for(Renting temp:allRentings){
            if(temp.getHouseId() == houseId){
                res.add(temp);
            }
        }

        HashMap<String,Object> allRentingsByHouseId = new HashMap<>();
        allRentingsByHouseId.put("allRentings",res);
        return allRentingsByHouseId;
    }

    public Map<String, Object> getAgentRenting() {
        HashMap<String,Object> map=new HashMap<>();
        map.put("agentRentings",rentingMapper.getAgentRenting());
        map.put("noagentRentings",rentingMapper.getNoAgentRenting());
        return map;
    }
}
